from dr_reeborg_api import *


def new_object_here():
    if not object_here():
        return 0
    elif object_here():
        num = 0
        while object_here():
            take()
            num += 1
        for i in range(num):
            put()
        return num


def turn_right():  # 右转
    turn_left()
    turn_left()
    turn_left()


def shift():  # 切位置
    move()
    turn_left()
    move()
    turn_right()


def square(steps):  # 走一个方形
    orientation = 4
    while orientation > 0:
        for i in range(steps):
            object_take()
            move()
        turn_left()
        orientation -= 1


def object_take():  # 收割胡萝卜
    if distance <= 5:
        num = new_object_here()
        if num == 0:
            put()
        while num > 1:
            take()
            num -= 1
    # while object_here():
    #     take()
    # else:
    #     if distance <= 5:
    #         put()


distance = 9
while distance >= 0:
    square(distance)
    shift()
    distance -= 2


"""
set_max_nb_steps(2000)

def new_object_here():
    if not object_here():
        return 0
    elif object_here():
        num = 0
        while object_here():
            take()
            num += 1
        for i in range(num):
            put()
        return num


def turn_right():  # 右转
    turn_left()
    turn_left()
    turn_left()


def shift():  # 切位置
    move()
    turn_left()
    move()
    turn_right()


def square(steps):  # 走一个方形
    orientation = 4
    while orientation > 0:
        for i in range(steps):
            object_take()
            move()
        turn_left()
        orientation -= 1


def object_take():  # 收割胡萝卜
    if distance <= 5:
        numbers = new_object_here()
        if numbers == 0:
            put()
        while numbers > 1:
            take()
            numbers -= 1
    # while object_here():
    #     take()
    # else:
    #     if distance <= 5:
    #         put()


distance = 9
while distance >= 0:
    square(distance)
    shift()
    distance -= 2


"""
